package com.bigcat.gulimall.demo.utils;
import com.google.protobuf.ByteString;
import io.netty.util.internal.StringUtil;
import org.springframework.util.StringUtils;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class ByteUtils {

    /**
     * 1字节数据转int类型数据
     */
    public static String toIntStr(byte b) {
        return String.valueOf(toInt(b));
    }

    /**
     * 1字节数据转int类型数据
     */
    public static int toInt(byte b) {
        return (b & 0xFF);
    }

    /**
     * byte[]转int
     */
    public static String toIntStr(byte[] bytes) {
        return String.valueOf(toInt(bytes));
    }

    /**
     * byte[]转int
     */
    public static int toInt(byte[] bytes) {
        int value = 0;
        // 由高位到低位
        for (int i = 0; i < bytes.length; i++) {
            int shift = (bytes.length - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;// 往高位游
        }
        return value;
    }

    /**
     * 获取指定长度byte数据转16进制
     */
    public static String toHex(byte[] value) {
        return toHex(value,null);
    }

    /**
     * 获取指定长度byte数据转16进制
     */
    public static String toHex(byte[] value, String a) {
        StringBuilder sb = new StringBuilder(value.length);
        for (byte b : value) {
            sb.append(String.format("%02x", b));
            if(!StringUtil.isNullOrEmpty(a)){
                sb.append(a);
            }
        }
        if(StringUtil.isNullOrEmpty(a)){
            return sb.toString();
        }
        return sb.toString().substring(0,sb.toString().length()-1);
    }

    // 倒敘
    public static String hexReverseOrder(byte[] value,String a) {
        StringBuilder sb = new StringBuilder(value.length);
        for (int i = value.length - 1; i >= 0; i--) {
            sb.append(String.format("%02x", value[i]));
            if(!StringUtil.isNullOrEmpty(a)){
                sb.append(a);
            }
        }
        if(StringUtil.isNullOrEmpty(a)){
            return sb.toString();
        }
        return sb.toString().substring(0,sb.toString().length()-1);
    }

    /**
     * 任意长度byte数组转long类型
     */
    public static long toLong(byte[] bytes) {
        long value = 0;
        for (int count = 0; count < bytes.length; ++count) {
            int shift = (bytes.length - 1 - count) << 3;
            value |= ((long) 0xff << shift) & ((long) bytes[count] << shift);
        }
        return value;
    }

    /**
     * 任意长度byte数组转long类型
     */
    public static String toLongStr(byte[] bytes) {
        return String.valueOf(toLong(bytes));
    }

    /**
     * 16进制字符串转byte数组
     */
    private static byte[] toBytes(String hexStr) {
        byte[] destByte = new byte[hexStr.length() / 2];
        int j = 0;
        for (int i = 0; i < destByte.length; i++) {
            byte high = (byte) (Character.digit(hexStr.charAt(j), 16) & 0xff);
            byte low = (byte) (Character.digit(hexStr.charAt(j + 1), 16) & 0xff);
            destByte[i] = (byte) (high << 4 | low);
            j += 2;

        }
        return destByte;
    }

    /**
     * 获取指定长度byte数据转String
     */
    public static String toString(byte[] value) {
        return new String(value);
    }

    /**
     * byte数组拼接
     */
    public static byte[] concatAll(byte[] first, byte[]... rest) {

        int totalLength = first.length;

        for (byte[] array : rest) {
            totalLength += array.length;
        }

        byte[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;

        for (byte[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }

        return result;
    }

//    /**
//     * short类型转大端byte数组
//     */
//    public static byte[] toBytes(short value) {
//        return ByteBuffer.allocate(2).order(ByteOrder.BIG_ENDIAN).putShort(value).array();
//    }

    /**
     * short类型byte数组
     */
    public static byte[] toBytes(short value) {
        return ByteBuffer.allocate(2).putShort(value).array();
    }

    /**
     * int类型转byte数组
     */
    public static byte[] toBytes(int value) {
        return ByteBuffer.allocate(4).putInt(value).array();
    }

    /**
     * 10进制转1字节16进制,不足两位补零
     */
    public static String toHex(int number) {
        String hex = String.format("%2s", Integer.toHexString(number));
        hex = hex.replaceAll("\\s", "0");
        return hex;
    }

    /**
     * 10进制转1字节16进制,不足两位补零
     */
    public static String toHex(String number) {
        String hex = String.format("%2s", Integer.toHexString(Integer.parseInt(number)));
        hex = hex.replaceAll("\\s", "0");
        return hex;
    }

    /**
     * 获取指定长度byte数组
     */
    public static byte[] getBytes(ByteBuffer buf, int len) {
        byte[] value = new byte[len];
        buf.get(value);
        return value;
    }

    /**
     * 八进制转16进制
     * @param value
     * @return
     */
    public static String octToHex(String value) {
        if (!StringUtils.hasText(value)){
            return null;
        }
        Integer octValue = Integer.valueOf(value, 8);
        return Integer.toHexString(octValue);
    }

    /**
     * 16进制mac逆向转化为byteString字符串
     *
     * mac: EB:09:A9:EB:C6:0A
     * @param mac
     * @return
     */
    public static String hexMacToByteString(String mac) {
        if (StringUtils.isEmpty(mac) || !mac.contains(":")) {
            return null;
        }
        String replaceMac = mac.replace(":", "").toLowerCase();
        byte[] bytes = ByteUtils.toBytes(replaceMac);
        String macNew = ByteUtils.hexReverseOrder(bytes, "");
        byte[] bytesNew = ByteUtils.toBytes(macNew);
        int max = Integer.MIN_VALUE;
        for (int i : bytesNew) {
            if (i > max){
                max = i;
                System.out.println(max);
            }
        }
        ByteString byteString = ByteString.copyFrom(bytesNew);
        String strByteString = byteString.toString();
        if (strByteString.length() < 20) {
            return null;
        }
        return strByteString.substring(strByteString.lastIndexOf("=\"") + 2, strByteString.lastIndexOf("\""));
    }

    public static void main(String[] args) {
        //每一位为八进制字符串
        String[] split = "d 000 000 000 000".split(" ");
        String mac = "";
        for (int i = split.length - 1; i >= 0; i--) {
            String s1 = octToHex(split[i]);
            if (StringUtils.hasText(s1)) {
                if (s1.length() == 1){
                    mac = mac.concat(":0").concat(s1);
                }else if (StringUtils.isEmpty(mac)){
                    mac = s1;
                }else {
                    mac = mac.concat(":").concat(s1);
                }
            }
        }
        System.out.println(mac.toUpperCase());
        String macs = hexMacToByteString(mac.toUpperCase());
        System.out.println(macs);
    }

}
